﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using DevComponents.AdvTree;
using NanoERP.Infrastructure;
using NanoERP.Model;
using NanoERP.Common;
using NanoERP.Business.BaseInfo;

namespace NanoERP
{
    [Module("FrmStockSet")]
    public partial class FrmStockSet : ModuleForm
    {
        readonly SysdictableBLL _sysdictablebLl;
        readonly GoodsinfoBLL _goodsInfoBll;
        readonly List<Goodsinfo> _listGoodsInfo;
        public FrmStockSet()
        {
            InitializeComponent();

            _goodsInfoBll = new GoodsinfoBLL();
            _sysdictablebLl = new SysdictableBLL();
            _listGoodsInfo = new List<Goodsinfo>();

            BindTreeView();
        }

        /// <summary>
        /// 
        /// </summary>
        void QueryGoods()
        {
            if (dicTree.SelectedNode == null) return;
            var id = int.Parse(dicTree.SelectedNode.Name);
            dgvList.AutoGenerateColumns = false;
            dgvList.DataSource = _goodsInfoBll.Query(id, txtKey.Text.Trim());
        }

        /// <summary>
        /// 绑定商品类别树
        /// </summary>
        void BindTreeView()
        {
            dicTree.Nodes.Clear();

            var sysdic = _sysdictablebLl.Get((int)SysDicEnum.商品信息);
            var firNode = new Node(sysdic.Dicname) { Tag = sysdic, Name = sysdic.Id.ToString(), Image = NanoERP.Properties.Resources.XmlStructure };
            firNode.ExpandAll();
            var firList = _sysdictablebLl.Query(sysdic.Id);
            RecursiveLoopNodes(firList, firNode);

            dicTree.Nodes.Add(firNode);
            if (dicTree.Nodes.Count > 0) dicTree.SelectedNode = dicTree.Nodes[0];
        }

        /// <summary>
        /// 递归循环调用
        /// </summary>
        /// <param name="firList">一级树集合</param>
        /// <param name="parentNode">上级节点</param>
        void RecursiveLoopNodes(List<Sysdictable> firList, Node parentNode)
        {
            firList.ForEach(r =>
            {
                var secNode = new Node(r.Dicname) { Tag = r, Name = r.Id.ToString(), Image = NanoERP.Properties.Resources.CreateTableTemplatesGallery };

                parentNode.Nodes.Add(secNode);
                var recList = _sysdictablebLl.Query(r.Id);
                if (recList.Count > 0)
                    RecursiveLoopNodes(recList, secNode);
            });
        }

        private void tsbPrint_Click(object sender, EventArgs e)
        {

        }

        private void tsbExport_Click(object sender, EventArgs e)
        {
            string msgLog = string.Empty;
            ExcelUtil.ExportToExcel(this.dgvList, "商品信息", out msgLog);
            if (msgLog != null)
            {
                MessageBox.Show(msgLog, "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            QueryGoods();
        }

        private void dicTree_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            QueryGoods();
        }

        private void dgvList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            ControlUtil.DataGridRowPostPaint(sender, e);
        }

        private void tsbBatSet_Click(object sender, EventArgs e)
        {
            if (dgvList.CurrentRow == null) return;

            var currRow = dgvList.CurrentRow.DataBoundItem as Goodsinfo;
            if (MessageBox.Show(string.Format("确定要设置商品【{0}】的库存上下线吗？", currRow.GoodsName), "提示信息",
                MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                var f = new FrmStockLimit(currRow);
                if (f.ShowDialog() == DialogResult.OK)
                {
                    currRow.LimitHnum = ((Goodsinfo)f.Tag).LimitHnum;
                    currRow.LimitLnum = ((Goodsinfo)f.Tag).LimitLnum;

                    _goodsInfoBll.Edit(currRow);
                }
            }

        }

        private void tsbSave_Click(object sender, EventArgs e)
        {
            _listGoodsInfo.ForEach(g => _goodsInfoBll.Edit(g));
            _listGoodsInfo.Clear();
            MessageBox.Show("数据存储成功！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void dgvList_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1 || e.ColumnIndex == -1) return;

            var row = dgvList.CurrentRow.DataBoundItem as Goodsinfo;

            if (!_listGoodsInfo.Exists(g => g.Id.Equals(row.Id)))
                _listGoodsInfo.Add(row);

        }
    }
}
